package fz.vrd.ui.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;


public class MyScrollView extends ScrollView {

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					if (view.getMeasuredHeight() <= getScrollY() + getHeight()
							&& getScrollY() != 0) {
						if(onScrollListener!=null)
							onScrollListener.onButtom();
					} else if (getScrollY() == 0) {
						if(onScrollListener!=null)
							onScrollListener.onTop();
					} else {
						if(onScrollListener!=null)
							onScrollListener.onScroll();
					}
				}
			}
		};
		setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				view = getChildAt(0);
				if (MotionEvent.ACTION_UP == event.getAction()) {
					handler.sendEmptyMessage(1);
				}
				return false;
			}
		});
	}

	public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private OnScrollListener onScrollListener;
	private Handler handler;
	private View view;

	public void setOnScrollListener(OnScrollListener onScrollListener) {
		this.onScrollListener = onScrollListener;
	}
	public interface OnScrollListener {
		/**
		 * ScrollView 已经滑到最下方
		 */
		public void onButtom();
		/**
		 * ScrollView 已经滑到顶部
		 */
		public void onTop();
		/**
		 * ScrollView 开始下滑
		 */
		public void onScroll();

	}
}
